import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);


function storeLocalStore (state) {
  window.localStorage.setItem("userMsg",JSON.stringify(state));
}

export default new Vuex.Store({
  state: {
    leftNavList: [
      {
        name: "Task Managment",
        show: true,
        path: "/user/table",
        nameA: "table",
      },
      {
        name: "Account Managment",
        show: false,
        path: "/user/account",
        nameA: "account",
      },
    ],
    showAccount: false,
    login: false, // 是否登录成功
    loginSucInfo: {
      ...JSON.parse(localStorage.getItem('login'))
    }, // 登录成功以后添加数据
    saveUserInfo: {
      name: "",
      email: "",
      phone: "",
      gender: "",
      country: "",
      workOrstudy: "",
      sisu: "",
    }, // 保存个人信息
    download_link: {}, // 下载的地址，setting 成功以后返回的http链接
    tableList: [], // table 里面的数据
    btnList: [
      {
        newName: "Upload",
        show: true,
        path: "/tools/upload",
        name: "upload",
      },
      {
        newName: "Setting",
        show: false,
        path: "/tools/set",
        name: "set",
      },
      {
        newName: "Download",
        show: false,
        path: "/tools/download",
        name: "download",
      },
    ],
    uploadDataBid: null, // 文件上传成功返回的数据,
    highlightHelp: null, // 高亮 help 页面
  },
  mutations: {
    // user 和 账号页面切换
    handleUserAcc(state, payload) {
      const { index, router } = payload;
      state.leftNavList.map((i, j) => {
        if (j === index) {
          router.push({
            name: i.nameA,
            path: i.path,
          });
          return { ...(i.show = true) };
        } else {
          return { ...(i.show = false) };
        }
      });

      if (index === 1) {
        state.showAccount = true;
      } else {
        state.showAccount = false;
      }
      storeLocalStore (state)
    },

    // 登录成功以后修改数据
    handleLogin(state, payload) {
      const { router, data } = payload;
      state.login = true;
      state.loginSucInfo = data;
      router.push({ path: "/" });
      // console.log("suc", payload);
      storeLocalStore (state)
    },
    // 退出登录
    handleLogout(state, payload) {
      // console.log('payload', payload)
      state.login = payload;
      state.loginSucInfo = {}
      storeLocalStore (state)
    },

    // 修改个人信息，保存到 vuex 里面
    handleSaveUserInfo(state, payload) {
      state.saveUserInfo = payload;
      storeLocalStore (state)
    },
    // setting 成功以后返回的链接
    handleDownloadLink(state, payload) {
      state.download_link = payload;
      storeLocalStore (state)
    },
    // 登录成功以后，保存到 table 里面的数据
    handleTableList(state, payload) {
      // console.log('payload', payload)
      state.tableList = payload;
      storeLocalStore (state)
    },
    // 切换 tools 页面的设置面板
    handleSetPage(state, payload) {
      const { item, index, router  } = payload || {}
      // console.log("payload", item, index, router);

      state.btnList.map((i, d) => {
          if(d === index ) {
              item.path && router.push(
                  {
                      path: item.path,
                      name: item.name
                  })
              return ({ ...i.show = true })
          } else {
              return ({ ...i.show = false })
          }
      });
      storeLocalStore (state)
    },
    // 文件上传成功返回的数据
    handleUploadData(state, payload) {
      state.uploadDataBid = payload
      storeLocalStore (state)
    },
    // help 页面高亮
    handleHighlightHelp(state, payload) {
      state.highlightHelp = payload
    },
    // 清除 tool 文件中所有的緩存
    handleClearMemory(state, payload) {
      state.uploadDataBid = null;
      state.download_link = {};
    }
  },
});
